<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 2017/9/5
 * Time: 17:06
 */

namespace app\admin\controller;

use app\admin\model\MenuModel;
use Think\Db;
use think\Request;


class Menu extends Common
{


    protected $menu_model;

    public function __construct() {
        parent::__construct();
        /* @var $menu_model \Admin\Model\AdminMenuModel */

        $menu_model = new MenuModel();
        $this->menu_model = $menu_model;
    }

    /**
     * @description:菜单首页
     */
    public function index() {

//        $a = $this->rebuild_tree('-1','1');

        //$result = array();
        //$result = $this->menu_model->selectAllMenu();//show_bug($result);
        $result = $this->menu_model->getMenuWithLine();

        //show_bug($result);

//        $menu = get_column($menu);
        $this->assign('menu', $result);
        return $this->view->fetch();
    }

    public function tree (){
        $this->rebuild_tree('-1','0');
    }

    /**
     * 自动构建left和right值
     * @param $parent
     * @param $left
     * @return mixed
     */
    public function rebuild_tree($parent,$left) {

        /*
        $parent = input('get.parent');
        $left = input('get.left');
        $conn = Db::connect();

        // the right value of this node is the left value + 1
        $right = $left+1;
        // get all children of this node

//        $result = mysqli_query($conn,$sql);
        var_dump($result2);die;
        while ($row = mysqli_fetch_array($result,MYSQLI_NUM)) {
            // recursive execution of this function for each
            // child of this node
            // $right is the current right value, which is
            // incremented by the rebuild_tree function
            $right = rebuild_tree($row['1'], $right,$conn);
            //var_dump($row);die;
        }
        // we've got the left value, and now that we've processed
        // the children of this node we also know the right value
        $sql2 = "UPDATE app_auth_rule SET l = '" . $left . "', r= '" . $right . "' WHERE id = '" . $parent . "' ;";
        mysqli_query($conn,$sql2);
        // return the right value of this node + 1
        return $right + 1;
        */



        $right = $left+1;

        $sql = "SELECT title,id FROM app_auth_rule WHERE pid = '" . $parent . "';";
        $result2 = Db::query($sql);

        //$ro = array() ;
            $row = $result2?$result2[0]:'';
            //$this->ro = $r ;
        //show_bug($r);
        while ($this->row = $result2) {
            show_bug($row);
            $right = $this->rebuild_tree($row['id'], $right);
        }


        $b = Db::name('auth_rule')->where('id',$parent)->update(array('l'=>$left,'r'=>$right));

        return $right + 1;




    }

    public function rebuild_tree_old($parent, $left) {
        // the right value of this node is the left value + 1
        $right = $left+1;
        // get all children of this node
        $result = mysql_query("
        SELECT name
        FROM tree
        WHERE parent = '" . $parent . "'
        ;"
        );
        while ($row = mysql_fetch_array($result)) {
            // recursive execution of this function for each
            // child of this node
            // $right is the current right value, which is
            // incremented by the rebuild_tree function
            $right = rebuild_tree($row['name'], $right);
        }
        // we've got the left value, and now that we've processed
        // the children of this node we also know the right value
        mysql_query("
        UPDATE tree
        SET
            lft = '" . $left . "',
            rgt= '" . $right . "'
        WHERE name = '" . $parent . "'
        ;"
        );
        // return the right value of this node + 1
        return $right + 1;

    }

    /**
     * header菜单
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getTopMenu() {

        $result = $this->menu_model->getTopMenu();

        return $result;
    }

    /**
     * 左边菜单导航
     *
     */
    public function subMenu() {
        $menu_model = new MenuModel();
        //show_bug(Request::instance()->param());die;
        $pid = input('get.moduleId');
        $subMenuList = $menu_model->getSubMenu($pid);

        echo json_encode($subMenuList);
    }


    /**
     * @description:添加菜单
     */
    public function addMenu() {
        if (Request::instance()->isPost()) {
            $data = input('post.');

            $data['is_menu'] = 1;
            //添加的菜单是否是三级菜单
            /*if ($data['pid']) {
                $data['is_menu'] = $this->menu_model->isSecondaryMenu($data['pid']) ? 2 : 1;
            }*/

            if ($this->menu_model->isExistOpt($data['url'])) {
                $this->ajaxerror("该菜单已存在");
            }

            if ($this->menu_model->add($data)){
                $this->ajaxSuccess("添加成功！");
            }else{
                $this->ajaxError("菜单添加失败");
            }

            /*if ($this->menu_model->addAdminMenu($data)) {
                $this->ajaxSuccess("菜单添加成功");
            } else {
                $this->ajaxError("菜单添加失败");
            }*/
        } else {
            $id = input('get.id');

            $this->assign('id', $id);
            return $this->fetch();
        }
    }

    /**
     * @description:更新菜单
     */
    public function editMenu() {
        if (Request::instance()->isPost()) {
            $data = input('post.');


            if ($this->menu_model->isExistOpt($data['url'], $data['id'])) {
                $this->ajaxerror("该菜单已存在");
            }

            $result = $this->menu_model->editAdminMenu($data);
            if ($result !== false) {
                $this->ajaxSuccess('更新成功');
            } else {
                $this->ajaxError('更新失败');
            }
        } else {
            $id = input('get.id');

            $menu_info = $this->menu_model->selectMenuById($id);
            $menuList = $this->menu_model->getMenuWithLine();
            $this->assign('menu_info', $menu_info);
            $this->assign('menuList', $menuList);
            $this->assign('opt', explode('/', $menu_info['url']));
            return $this->fetch();
        }
    }

    /**
     * @description:删除菜单
     */
    public function deleteMenu() {
        $id = input('post.id');

        if ($this->menu_model->isExistSonMenu($id)) {
            $this->ajaxError('存在子菜单未删除');
        }

        if (!$this->menu_model->deleteAdminMenu($id)) {
            $this->ajaxError('删除失败');
        } else {
            $this->ajaxSuccess('删除成功');
        }
    }

    /**
     * @description:查看三级操作
     */
    public function viewOpt() {
        $id = input('get.uuid');

        $_opts = $this->menu_model->selectOpt($id);

        if (!count($_opts)) {
            $this->ajaxError('该菜单还未添加任何操作');
        }

        $this->assign('opts', $_opts);
        return $this->fetch();
    }



}